package com.scj.vue3springboot.controller.sys;

import com.scj.vue3springboot.entity.SysUser;
import com.scj.vue3springboot.entity.vo.LoginVo;
import com.scj.vue3springboot.entity.vo.UserRoleVo;
import com.scj.vue3springboot.service.SysUserService;
import com.scj.vue3springboot.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ShiChaojie
 * @date 2023/10/11 12:58
 */
@Api("用户接口")
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private SysUserService sysUserService;


    @ApiOperation("登录接口")
    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo) {
        return sysUserService.login(loginVo);
    }

    @ApiOperation("根据token获取用户信息接口")
    @GetMapping("/info")
    public Result info(@RequestHeader("token") String token) {
        return sysUserService.info(token);
    }

    @ApiOperation("获取所有用户信息接口")
    @GetMapping("/findAllUserInfo/{page}/{limit}")
    public Result findAllUserInfo(
            @PathVariable Integer page,
            @PathVariable Integer limit,
            @ModelAttribute SysUser sysUser) {
        return sysUserService.findAllUserInfo(page, limit, sysUser);
    }

    @ApiOperation("添加用户接口")
    @PostMapping("/addUser")
    public Result addUser(@RequestBody SysUser sysUser,@RequestHeader("token") String token) {
        return sysUserService.addUser(sysUser,token);
    }

    @ApiOperation("修改用户接口")
    @PutMapping("/updateUser")
    public Result updateUser(@RequestBody SysUser sysUser,@RequestHeader("token") String token) {
        return sysUserService.updateUser(sysUser,token);
    }

    @ApiOperation("删除用户接口")
    @DeleteMapping("/deleteUser/{userId}")
    public Result deleteUser(@PathVariable Long userId,@RequestHeader("token") String token) {
        return sysUserService.deleteUser(userId,token);
    }

    @ApiOperation("批量删除用户接口")
    @DeleteMapping("/deleteUsers")
    public Result deleteUsers(@RequestBody List<Long> idList, @RequestHeader("token") String token) {
        return sysUserService.deleteUsers(idList,token);
    }

    @ApiOperation("根据用户id查询所有角色信息以及自己拥有的角色信息接口")
    @GetMapping("/findAllRoleAndUserRole/{userId}")
    public Result findAllRoleAndUserRole(@PathVariable Long userId) {
        return sysUserService.findAllRoleAndUserRole(userId);
    }

    @ApiOperation("根据用户id添加角色信息接口")
    @PostMapping("/addUserRole")
    public Result addUserRole(@RequestBody UserRoleVo userRoleVo) {
        return sysUserService.addUserRole(userRoleVo.getUserId(),userRoleVo.getRoleIdList());
    }

}
